package com.lfg.leetcode.leetcode300;


public class No413numberOfArithmeticSlices {
    public static void main(String[] args) {
        System.out.println(numberOfArithmeticSlices(new int[]{1, 2, 3, 4}));
    }

    public static int numberOfArithmeticSlices(int[] nums) {
        if(nums.length < 3){
            return 0;
        }
        int ret = 0;
        int d = nums[1] - nums[0];
        int cnt = 2;
        for (int i = 2; i < nums.length; i++) {
            if (nums[i] - nums[i - 1] == d) {
                cnt++;
            } else {
                d = nums[i] - nums[i - 1];
                ret = ret + (cnt - 1) * (cnt - 2) / 2;
                cnt = 2;
            }
        }
        if (cnt > 2) {
            ret = ret + (cnt - 1) * (cnt - 2) / 2;
        }

        return ret;
    }

}



